package club.mzywucai.blog.air_portal.service.impl;

import club.mzywucai.blog.air_portal.mapper.FileInfoMapper;
import club.mzywucai.blog.air_portal.pojo.domain.JsonData;
import club.mzywucai.blog.air_portal.pojo.po.FileInfoPo;
import club.mzywucai.blog.air_portal.pojo.vo.DownloadVo;
import club.mzywucai.blog.air_portal.pojo.vo.FileExistenceVo;
import club.mzywucai.blog.air_portal.service.GetFileInfoService;
import club.mzywucai.blog.air_portal.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author mzywucai
 * @Description
 * @date 2019/6/2
 */
@Service
public class GetFileInfoServiceImpl implements GetFileInfoService {
    @Autowired
    FileInfoMapper fileInforMapper;

    @Override
    public JsonData checkFileExistenceAndHasPass(String fileCode) {
        boolean fileExistence = false;
        boolean fileHasPass = false;

        Integer fileId = fileInforMapper.getFileId(fileCode);
        String filePass = fileInforMapper.getFilePass(fileCode);

        if (null != fileId && fileId != 0) {
            fileExistence = true;
        }
        if (null != filePass && !"".equals(filePass)) {
            fileHasPass = true;
        }

        FileExistenceVo fileExistenceVo = new FileExistenceVo();
        fileExistenceVo.setFileExistence(fileExistence);
        fileExistenceVo.setFileHasPass(fileHasPass);

        return JsonData.buildSuccess(0, fileExistenceVo, "操作成功");
    }

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public FileInfoPo getFileInfo(String fileCode) {
        int fileTimes = fileInforMapper.getFileTimes(fileCode);

        boolean flag = true;
        FileInfoPo file = null;
        // 为0时，删除这个文件了，然后不能查了。
        if (fileTimes == 0) {
            int fileId = fileInforMapper.getFileId(fileCode);
            fileInforMapper.delFile(fileId);
            flag = false;
        }

        // 不为0，查出，并且要记得减1。
        if (flag) {
            file = fileInforMapper.getFile(fileCode);
            fileInforMapper.subFileTimes(fileCode);
        }

        return file;
    }

    @Override
    public JsonData getFileURI(String fileCode, String password) {
        if (fileCode == null && password == null) {
            return JsonData.buildError("empty word");
        }

        password = password.trim();
        String filePassFromDB = fileInforMapper.getFilePass(fileCode);

        FileInfoPo fileInfo = null;
        String fileURI = null;
        String fileOriginName = null;
        if ("".equals(filePassFromDB)) {
            fileInfo = getFileInfo(fileCode);
        }

        if (!"".equals(filePassFromDB) &&
                MD5Util.checkPass(password, filePassFromDB)) {
            fileInfo = getFileInfo(fileCode);
        }

        if (null != fileInfo) {
            fileURI = fileInfo.getFileURI();
            fileOriginName = fileInfo.getFileOriginName();
        }

        if (null == fileURI) {
            return JsonData.buildError(-1, "get uri fail.");
        } else {
            DownloadVo downloadVo = new DownloadVo();
            downloadVo.setFileURI(fileURI);
            downloadVo.setFileOriginName(fileOriginName);

            return JsonData.buildSuccess(0, downloadVo, "success");
        }
    }


}
